//
//  MCLogOffVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/7.
//

import UIKit
import SnapKit
import QMUIKit
import SnapKitExtend
import Toast_Swift


class MCLogOffVc: MCBaseVc {
    
    var logOffTF:QMUITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "注销账号"
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
         loadSubViews()
    }
    
    func loadSubViews() -> Void{
        let logoffLabel = QMUILabel.init()
        logoffLabel.text = "根据《中华人民共和国网络安全法》的要求，所有有账号体系的 APP 都必须提供账号注销的功能.\n\nMaster 为了保障用户的权益，特别开通了“账号注销”功能。该功能将会永久删除您的账户、数据、会员等所有信息，且无法恢复，请您在注销前慎重考虑。一旦注销成功，您的账户和账户内的所有资料将会被彻底清除，并且不可恢复。\n\n如果您已经决定要注销账号，请在下面的输入框中输入：\n"
        logoffLabel.textColor = UIColor.qmui_color(withHexString: "#10151C")
        logoffLabel.font = UIFont(name: "PingFangSC-Regular", size: 15)
        logoffLabel.numberOfLines = 0
        view.addSubview(logoffLabel)
        logoffLabel.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(16)
            make.right.equalTo(view.snp.right).offset(-16)
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(25)
        }
        
        let offLabel = QMUILabel.init()
        offLabel.text = "我遵守中华人民共和国网络安全法自愿注销账号"
        offLabel.font = UIFont(name: "PingFangSC-Semibold", size: 15)
        offLabel.textColor = UIColor.qmui_color(withHexString: "#10151C")
        offLabel.numberOfLines = 0
        view.addSubview(offLabel)
        offLabel.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(16)
            make.right.equalTo(view.snp.right).offset(-16)
            make.top.equalTo(logoffLabel.snp.bottom).offset(10)
        }
        
        let logoffTextField = QMUITextField.init()
        logoffTextField.layer.cornerRadius = 5
        logoffTextField.layer.masksToBounds = true
        logoffTextField.layer.borderColor = UIColor.qmui_color(withHexString: "#E6E6E6")?.cgColor
        logoffTextField.layer.borderWidth = 1
        view.addSubview(logoffTextField)
        logoffTextField.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(16)
            make.right.equalTo(view.snp.right).offset(-16)
            make.top.equalTo(offLabel.snp.bottom).offset(10)
            make.height.equalTo(50)
        }
        
        logOffTF = logoffTextField
        
        let logoffBtnLabel = QMUILabel.init()
        logoffBtnLabel.text = "然后点击下面的「注销账号」按钮进行注销。"
        logoffBtnLabel.textColor = UIColor.qmui_color(withHexString: "#10151C")
        logoffBtnLabel.font = UIFont(name: "PingFangSC-Regular", size: 15)
        logoffBtnLabel.numberOfLines = 0
        view.addSubview(logoffBtnLabel)
        logoffBtnLabel.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(16)
            make.right.equalTo(view.snp.right).offset(-16)
            make.top.equalTo(logoffTextField.snp.bottom).offset(10)
        }
        
        
        let logoffBtn = QMUIButton.init()
        logoffBtn.setTitle("确定注销", for: .normal)
        logoffBtn.backgroundColor = UIColor.qmui_color(withHexString: "#35DE94")
        logoffBtn.titleLabel?.font = UIFont(name: "PingFangTC-Semibold", size: 15)
        logoffBtn.layer.cornerRadius = 25
        logoffBtn.layer.borderWidth = 1.0
        logoffBtn.setTitleColor(UIColor.qmui_color(withHexString: "#10151C"), for: .normal)
        logoffBtn.layer.borderColor = UIColor.qmui_color(withHexString: "#FFFFFF")?.cgColor
        logoffBtn.addTarget(self, action: #selector(loadLogoffSender), for: .touchUpInside)
        view.addSubview(logoffBtn)
        logoffBtn.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(16)
            make.right.equalTo(view.snp.right).offset(-16)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-30)
            make.height.equalTo(50)
        }
    }
        
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.view.endEditing(true)
    }
    
    @objc func loadLogoffSender() -> Void {
        if logOffTF.text?.isEmpty == true {
            self.view.makeToast("注销文字不能为空",position: .center)
        }
      
        if logOffTF.text == "注销账号"{
            logoff()
        }else {
            self.view.makeToast("文字输入不对",position: .center)
        }
        
        
    }
    
    
    //注销
    func logoff() -> Void {
        NetWorkRequest(MCAccountApi.logOff) { ResponseModel in
            
            if ResponseModel.code == 200 {
                self.view.makeToast("注销成功",position: .center)
                UserDefaults.standard.removeObject(forKey: "LoginToken")
                UserDefaults.standard.removeObject(forKey: "LoginName")
                let appdelete:AppDelegate = UIApplication.shared.delegate as! AppDelegate
                appdelete.Login()
            }else
            {
                self.view.makeToast("注销失败",position: .center)
            }
            
            
        } failureCallback: { ResponseModel in
            self.view.makeToast("注销失败",position: .center)
        }
    }
    
    
    
    
}
